const { exec } = require('child_process');
const path = require('path');

// 确保在正确的目录下运行
const rootDir = path.resolve(__dirname, '..');

console.log('\n正在运行 FastEasy 库性能测试...\n');

// 执行 TypeScript 编译
const compileProcess = exec(`cd ${rootDir} && npx tsc`, (error, stdout, stderr) => {
  if (error) {
    console.error(`编译失败: ${error.message}`);
    console.error(stderr);
    return;
  }

  console.log('编译成功，开始执行性能测试...\n');

  // 运行性能测试
  const testProcess = exec(
    `node ${path.join(rootDir, 'dist', 'performance-comparison', 'performance.test.js')}`,
    (testError, testStdout, testStderr) => {
      if (testError) {
        console.error(`测试执行失败: ${testError.message}`);
        console.error(testStderr);
        return;
      }

      console.log(testStdout);
      console.log('\n性能测试执行完毕！');
    }
  );
});

compileProcess.stdout.on('data', data => {
  console.log(data);
});

compileProcess.stderr.on('data', data => {
  console.error(data);
});
